package com.cweb.admin.model.sys.sys_role_info.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cweb.admin.model.sys.sys_role_info.entity.SysRoleInfo;
import com.cweb.admin.model.sys.sys_role_info.mapper.SysRoleInfoMapper;
import com.cweb.admin.model.sys.sys_user_to_role.entity.SysUserToRole;
import com.cweb.admin.model.sys.sys_user_to_role.service.SysUserToRoleService;
import com.cweb.framework.base.serivce.BaseMybatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统-系统角色表 服务实现类
 * @since 2020-08-17
 */
@Service
public class SysRoleInfoService extends BaseMybatisService<SysRoleInfoMapper, SysRoleInfo> {
    @Autowired
    private SysUserToRoleService sysUserToRoleService;

    /**
     * 取得角色的所有权限
     */
    @Cacheable(value = "emp", key = "targetClass+'MappingByRole'+#p0")
    public List<String> selectMappingByRole(Long roleId) {
        log.info("查询角色权限缓存" + roleId);
        // 取得角色的权限
        return this.getBaseMapper().selectMappingByRole(roleId);
    }

    @CacheEvict(value = "emp", key = "targetClass+'MappingByRole'+#p0")
    public void clearMappingByRole(Long roleId) {
        log.info("清空角色权限缓存" + roleId);
    }

    /**
     * 取得用户的所有角色
     */
    public String[] selectRoleByUid(Long uid) {
        List<SysUserToRole> listSysUserToRole = sysUserToRoleService.list(new QueryWrapper<>(new SysUserToRole() {{
            setUserId(uid);
        }}));
        // 取得角色的权限
        return listSysUserToRole.stream().map(v -> v.getRoleId()).toArray(String[]::new);
    }
}
